import Cookie from './cookies'
var vuexStorage = {
  storageKeys: [],
  sessionKeys: [],
  cookieKeys: [],
  store: null,
  init (storageKeys = [], sessionKeys = [], cookieKeys = []) {
    this.storageKeys = storageKeys
    this.sessionKeys = sessionKeys
    this.cookieKeys = cookieKeys
    return store => {
      this.$store = store
      if (this.storageKeys.length) {
        this.initStorage()
      }
      if (this.sessionKeys.length) {
        this.initSession()
      }
      if (this.cookieKeys.length) {
        this.initCookie()
      }
      store.subscribe((mutation, state) => {
        if (this.storageKeys.length) {
          this.storageAsync(state)
        }
        if (this.sessionKeys.length) {
          this.sessionAsync(state)
        }
        if (this.cookieKeys.length) {
          this.cookieAsync(state)
        }
      })
    }
  },
  initCookie () {
    let cookie = document.cookie
    cookie = Cookie.parse(cookie)
    if (!cookie.storeAsyc) return false
    let storage = JSON.parse(cookie.storeAsyc)
    if (cookie.storeAsyc && JSON.stringify(storage) !== '"{}"') {
      Object.entries(this.$store.state).forEach(item => {
        if (!this.cookieKeys.includes(item[0])) {
          storage[item[0]] = item[1]
        }
      })
      this.$store.replaceState(storage)
    }
  },
  cookieAsync (state) {
    if (!this.cookieKeys || this.cookieKeys.length === 0) return
    let storeSave = {}
    this.cookieKeys.forEach(function (val) {
      if (state[val]) {
        storeSave[val] = state[val]
      }
    })
    Cookie.set('storeAsyc', JSON.stringify(storeSave), Infinity, '/')
  },
  initStorage () {
    let localStorage = window.localStorage.getItem('storeAsync')
    localStorage = JSON.parse(localStorage)
    if (!this.storageKeys.length || !localStorage) return false
    let storage = {}
    Object.entries(this.$store.state).forEach(item => {
      if (this.storageKeys.includes(item[0])) {
        storage[item[0]] = localStorage[item[0]]
      } else {
        storage[item[0]] = item[1]
      }
    })
    this.$store.replaceState(storage)
  },
  initSession () {
    let localStorage = window.sessionStorage.getItem('storeAsync')
    localStorage = JSON.parse(localStorage)
    if (!this.sessionKeys.length || !localStorage) return false
    let storage = {}
    Object.entries(this.$store.state).forEach(item => {
      if (this.sessionKeys.includes(item[0])) {
        storage[item[0]] = localStorage[item[0]]
      } else {
        storage[item[0]] = item[1]
      }
    })
    this.$store.replaceState(storage)
  },
  storageAsync (state) {
    let storage = {}
    Object.entries(state).forEach(item => {
      if (this.storageKeys.includes(item[0])) {
        storage[item[0]] = item[1]
      }
    })
    window.localStorage.setItem('storeAsync', JSON.stringify(storage))
  },
  sessionAsync (state) {
    let storage = {}
    Object.entries(state).forEach(item => {
      if (this.sessionKeys.includes(item[0])) {
        storage[item[0]] = item[1]
      }
    })
    window.sessionStorage.setItem('storeAsync', JSON.stringify(storage))
  }
}
export default vuexStorage
